<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords">
          <el-input v-model="queryParams.keywords" placeholder="请输入关键字" clearable style="width: 200px" />
          <el-button type="primary" style="margin-left: 0; border-radius: 0" @click="handleQuery"> 搜索 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never">
      <template #header>
        <div class="justify-between">
          <el-button v-if="permissions.create" type="primary" @click="handelCreate"> 申请复核 </el-button>
        </div>
      </template>
      <el-table ref="tableRef" v-loading="loading" :data="tableList" size="small" highlight-current-row border>
        <el-table-column type="selection" width="55" />
        <el-table-column label="项目历史状态" align="center" prop="lockPrjStatusName" min-width="100">
          <template #default="scope">
            <span v-if="scope.row.prjStatus == 1">
              <el-text type="info"> {{ scope.row.prjStatusName }}复核 </el-text>
            </span>
            <span v-if="scope.row.prjStatus == 2">
              <el-text> {{ scope.row.prjStatusName }}复核 </el-text>
            </span>
            <span v-if="scope.row.prjStatus == 3">
              <el-text type="primary"> {{ scope.row.prjStatusName }}复核 </el-text>
            </span>
            <span v-if="scope.row.prjStatus == 4">
              <el-text type="warning"> {{ scope.row.prjStatusName }}复核 </el-text>
            </span>
            <span v-if="scope.row.prjStatus == 5">
              <el-text type="danger"> {{ scope.row.prjStatusName }}复核 </el-text>
            </span>
            <span v-if="scope.row.prjStatus == 6">
              <el-text type="success"> {{ scope.row.prjStatusName }}复核</el-text>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="当前项目状态" align="center" prop="prjStatusName" min-width="100"> </el-table-column>
        <el-table-column label="编号" align="center" prop="code" min-width="130" />
        <el-table-column label="项目名称" align="center" prop="projectName" min-width="130" />
        <el-table-column label="锁定原因" align="center" prop="lockReason" min-width="130" />
        <el-table-column label="复核月份" align="center" prop="month" min-width="130" />
        <el-table-column label="申请人" align="center" prop="unlockApplicant" min-width="130" />
        <el-table-column label="申请时间" align="center" prop="unlockApplicationTime" min-width="130" />
        <el-table-column label="申请理由" align="center" prop="unlockReason" min-width="130" />
        <el-table-column label="复核结果" align="center" prop="auditStatus" min-width="100">
          <template #default="scope">
            <el-tooltip
              v-if="scope.row.auditIdea === 1"
              :content="scope.row.auditIdeaContent"
              placement="top"
              effect="light"
            >
              <el-text type="primary">{{ scope.row.auditStatusName }} <el-text type="success">:通过</el-text></el-text>
            </el-tooltip>
            <el-tooltip
              v-else-if="scope.row.auditIdea === -1"
              :content="scope.row.auditIdeaContent"
              placement="top"
              effect="light"
            >
              <el-text type="primary">{{ scope.row.auditStatusName }} <el-text type="danger">:未通过</el-text></el-text>
            </el-tooltip>
            <el-text v-else type="warning">{{ scope.row.auditStatusName }}</el-text>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button
              v-if="permissions.detail"
              v-hasPerm="['projectLockLog:details']"
              type="primary"
              link
              size="small"
              @click="handelDetail(scope.row)"
            >
              详情
            </el-button>
            <el-button
              v-if="permissions.audit && scope.row.auditStatus === 0"
              v-hasPerm="['projectLockLog:audit']"
              link
              type="primary"
              @click="handelAudit(scope.row)"
            >
              复核
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>
    <reaudit v-if="dialogAudit.visible" :dialog-audit="dialogAudit" @send-close="handleSendClose"></reaudit>
    <detail v-if="dialogDetail.visible" :dialog-detail="dialogDetail" @send-close="handleSendClose"></detail>
    <reapply v-if="dialogApply.visible" :dialog-apply="dialogApply" @send-close="handleSendClose"></reapply>
  </div>
</template>

<script lang="ts" setup>
import { QueryProjectLockLogFH, DeleteProjectLockLog } from "@/api/admin/locklog/index";
import { downloadFile } from "@/utils";
import reapply from "./reapply.vue";
import reaudit from "./reaudit.vue";
import detail from "./detail.vue";
import { ElTable } from "element-plus";

const permissions = ref({
  create: false,
  audit: false,
  detail: false,
});

const currentSystem = ref(localStorage.getItem("usertypeName"));
const isLumpContractor = ref(localStorage.getItem("isLumpContractor"));

if (currentSystem.value == "SysUser") {
  permissions.value.audit = true;
  permissions.value.detail = true;
}
if (currentSystem.value == "Corp" || currentSystem.value == "Manager") {
  permissions.value.create = true;
  permissions.value.detail = true;
}

const total = ref(0);
const loading = ref(false);
const queryFormRef = ref(ElForm);
const tableRef = ref(ElTable);
const tableList = ref<any[]>();
const queryParams = reactive({
  pageNum: 1,
  pageSize: 20,
  keywords: "",
});

const dialogApply = reactive<DialogOption>({
  visible: false,
});

const dialogAudit = reactive<DialogOption>({
  visible: false,
});
const dialogDetail = reactive<DialogOption>({
  visible: false,
});

//复核
const handelCreate = () => {
  dialogApply.visible = true;
  dialogApply.title = "申请复核";
  dialogApply.footBtn = "确认提交";
};

const handelAudit = (row: any) => {
  dialogAudit.visible = true;
  dialogAudit.title = "复核";
  dialogAudit.footBtn = "确认提交";
  dialogAudit.id = row.id;
};

//详情
const handelDetail = (row: any) => {
  dialogDetail.visible = true;
  dialogDetail.title = "详情";
  dialogDetail.id = row.id;
};

// 关闭弹窗
const handleSendClose = (query: boolean) => {
  dialogAudit.visible = false;
  dialogDetail.visible = false;
  dialogApply.visible = false;
  if (query) {
    handleQuery();
  }
};

/* 列表查询 */
const handleQuery = async () => {
  loading.value = true;
  QueryProjectLockLogFH(queryParams)
    .then((res) => {
      tableList.value = res.data.list;
      queryParams.pageNum = res.data.pageNum;
      queryParams.pageSize = res.data.pageSize;
      total.value = res.data.total;
    })
    .finally(() => {
      loading.value = false;
    });
};

onMounted(() => {
  handleQuery();
});
</script>

<style lang="less" scoped>
:deep(.detail .el-dialog__body) {
  height: var(--mobileAddFormHeight);
  overflow-y: auto;
}
</style>
